package com.cqu.hhx.driver.controller;

        import com.cqu.hhx.driver.entity.Customer;
        import com.cqu.hhx.driver.entity.Driver;
        import com.cqu.hhx.driver.entity.forWeb.LoginReq;
        import com.cqu.hhx.driver.service.LoginService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;

        import java.util.HashMap;
        import java.util.Map;

/**
 * @author DJ
 * @create 2020-05-23 18:46
 * 登录控制器
 */
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 获取登录界面
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    /**
     * 获取管理员主界面
     * @return
     */
    @RequestMapping(value = "/managerHome",method = RequestMethod.GET)
    public String home(){
        return "manageHome";
    }

    /**
     * 管理员登录验证
     * @param loginReq
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/manager/login",method = RequestMethod.POST)
    public Map<String,Object> checkManagerLogin(@RequestBody LoginReq loginReq){
        Map<String,Object> map=new HashMap<>();
        Integer status=loginService.checkManagerLogin(loginReq.getAccount(),loginReq.getPassWord());
        //1：账户不存在 2：密码错误 0：登录成功
        map.put("status",status);
        return map;
    }

    /**
     * 乘客登录验证
     * @param loginReq
     * @return  登录成功返回：status和customer对象，登录失败返回：status
     */
    @ResponseBody
    @RequestMapping(value ="/customer/login",method = RequestMethod.POST)
    public Map<String,Object> checkCustomerLogin(@RequestBody LoginReq loginReq){
        Map<String,Object> map=new HashMap<>();
        Customer customer=loginService.checkCustomerLogin(loginReq.getAccount(),loginReq.getPassWord());
        if(customer!=null) {
            String password = loginReq.getPassWord();
            if (password.equals(customer.getPassWord())) {
                map.put("status", 200);
                map.put("customer", customer);
                return map;
            } else {
                map.put("status",1001);//密码不正确
                return map;
            }
        }else{
            map.put("status",1000);//账号不存在
            return map;
        }
    }

    /**
     * 司机登录验证
     * @param loginReq
     * @return  登录成功返回：status和driver对象，登录失败返回：status
     */
    @ResponseBody
    @RequestMapping(value = "/driver/login",method = RequestMethod.POST)
    public Map<String,Object> checkDriverLogin(@RequestBody LoginReq loginReq){
        Map<String,Object> map=new HashMap<>();
        Driver driver=loginService.checkDriverLogin(loginReq.getAccount(),loginReq.getPassWord());
        if(driver!=null){
            String password=loginReq.getPassWord();
            if(password.equals(driver.getPassWord())){
                map.put("status", 200);
                map.put("driver",driver);
                return map;
            }else{
                map.put("status",1001);//密码不正确
                return map;
            }
        }
        else{
            map.put("status",1000);//账号不存在
            return map;
        }
    }

    /**
     * 司机登录验证
     * @param account
     * @param passWord
     * @return 登录成功返回：status和driver对象，登录失败返回：status
     */
    @ResponseBody
    @RequestMapping(value = "/driver/android/login",method = RequestMethod.POST)
    public Map<String,Object> checkDriverLogin(@RequestParam String account,@RequestParam String passWord){
        Map<String,Object> map=new HashMap<>();
        Driver driver=loginService.checkDriverLogin(account,passWord);
        if(driver!=null){
            String password=passWord;
            if(password.equals(driver.getPassWord())){
                map.put("status", 200);
                map.put("driver",driver);
                return map;
            }else{
                map.put("status",1001);//密码不正确
                return map;
            }
        }
        else{
            map.put("status",1000);//账号不存在
            return map;
        }
    }
}
